package com.sr.vo.req;

import com.sr.pojo.SysRole;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * @author xiaochi
 * @date 2021/12/16 15:16
 * @desc RoleReqVo
 */
@ApiModel("角色实体")
@Data
@EqualsAndHashCode(callSuper = true)
public class RoleReqVo extends BaseForm<SysRole> {

    @ApiModelProperty("角色id，更新必须")
    @NotBlank(message = "角色 id 不能为空",groups = Update.class)
    @Size(min = 19,max = 19,message = "角色id不正确",groups = Update.class)
    private String id;

    @ApiModelProperty("角色名称")
    @NotBlank(message = "角色名称不能为空",groups = {Create.class,Update.class})
    @Length(min = 2,max = 15,message = "角色名称长度为{min} ~ {max}个字符",groups = {Create.class,Update.class})
    private String name;

    @Size(max = 255,message = "最大不能超过255个字符")
    private String description;// 角色描述

    @Range(min = 0,max = 1,message = "角色状态：1:正常0:弃用",groups = Update.class)
    @PositiveOrZero(message = "角色状态：1:正常0:弃用",groups = Update.class)
    private Integer status;// 角色状态：1:正常0:弃用

    private List<String> permissionsIds;// 选择的权限id集合

    /**
     * 获取实例
     * @return 返回实体类
     */
    @Override
    public SysRole buildEntity() {
        SysRole sysRole = ToolUtil.copy(this, SysRole.class);
        Date date = new Date();
        if (StringUtils.isEmpty(sysRole.getId())){
            sysRole.setId(ToolUtil.nextId());
            sysRole.setCreateTime(date);
            sysRole.setUpdateTime(date);
        }else{
            sysRole.setUpdateTime(date);
        }
        return sysRole;
    }
}
